﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AL100.BankApplication.BLL
{
    /// <summary>
    /// A local Bank.
    /// </summary>
    public class Bank : SingletonBase<Bank>, IVisitable
    {
        IList<AccountBase> _accounts;
      

        /// <summary>
        /// Private constructor to prevent instanciation.
        /// </summary>
        private Bank()
        {
            // On instancie la liste de comptes.
            _accounts = new List<AccountBase>();
        }


        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public IList<AccountBase> Accounts
        {
            get { return _accounts; }
            private set { _accounts = value; }
        }


        /// <summary>
        /// Creates the account.
        /// </summary>
        /// <param name="holder">The holder.</param>
        /// <param name="deposit">The deposit.</param>
        /// <returns>The newly created account</returns>
        public AccountBase CreateAccount(Customer holder, double deposit)
        {
            // Creates the Account..
            var account =  AccountFactory.Create(holder, deposit);

            // Adds the account the the Bank's Accounts List
            _accounts.Add(account);

            //  And subscribe the Bank to the account OverdrawnWithdrawal event.
            //  The two following lines are identical :
            //account.OverdrawnWithdrawal   += new EventHandler<AccountEventArgs>(OnAccountOverdrawnWithdrawal);
            account.OverdrawnWithdrawal     += OnAccountOverdrawnWithdrawal;

            // and return the Account.
            return account;
        }

        /// <summary>
        /// Called when an overdrawn withdrawal is performed on an Account..
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="AL100.BankApplication.BLL.AccountEventArgs"/> instance 
        /// containing the event data.</param>
        private void OnAccountOverdrawnWithdrawal(object sender, AccountEventArgs e)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates the account.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="deposit">The deposit.</param>
        /// <returns>The newly created account</returns>
        public AccountBase CreateAccount(string name, string firstName, double deposit)
        {
            return CreateAccount(new Customer(name, firstName), deposit);
        }



        /// <summary>
        /// Accepts the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public void Accept(IVisitor visitor)
        {
            // makes the visitor do its job !
            visitor.Visit(this);
        }
    }




}
